package com.hut.bms.controller;

import com.hut.bms.model.BookInfo;
import com.hut.bms.service.DeteleBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/BMS")
@RestController
public class DeleteBookController {
    @Autowired
    private DeteleBookService deteleBookService;
    //删除一本图书信息
    @RequestMapping(value = "/deleteBook",produces = "application/json")
    public String deleteBook(Integer bookId) {
        //打印从前端参数传入到后端的日志
        log.info("添加图书, 接收到参数bookInfo: {}", bookId);
        //校验参数
        if (bookId == null) {
            return "输入参数不合法";
        }
       //删除图书
        try {
            Integer line = deteleBookService.deleteBookInfoByStatus(bookId);
            if(line > 0) {
                return "";
            }
        }catch (Exception e) {
            log.error("删除图书失败，e: ",e);
        }
        return "删除失败";
    }
    //删除多本图书信息
    @RequestMapping(value = "/batchDeleteBook",produces = "application/json")
    public String batchDeleteBook(@RequestParam List<Integer> ids) {
        //打印从前端参数传入到后端的日志
        log.info("添加图书, 接收到参数bookInfo: {}", ids);
        //校验参数
        if (ids == null) {
            return "输入参数不合法";
        }
        try {
            Integer line = deteleBookService.batchDeleteBookInfo(ids);
            if(line > 0) {
                return "";
            }
        } catch (Exception e) {
            log.error("批量删除图书失败，e：",e);
        }
        return "批量删除失败";
    }
}
